﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormTaiKhoan : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private string idtam = "";
        public FormTaiKhoan()
        {
            InitializeComponent();
        }

        private void BttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BttLamMoi_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void BttXoa_Click(object sender, EventArgs e)
        {
            Delete();
            Reset();
        }

        private void BttCapNhap_Click(object sender, EventArgs e)
        {
            var f = new FormTaiKhoanCapNhap {id = idtam};
            f.ShowDialog();
        }

        private void BttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormTaiKhoanThem();
            f.ShowDialog();
        }
        // get idtam
        private void dgvTaiKhoan_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowindex = e.RowIndex;
            var row = dgvTaiKhoan.Rows[rowindex];
            idtam = row.Cells[0].Value.ToString();
            KiemTra();
        }

        private void dgvTaiKhoan_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowindex = e.RowIndex;
            var row = dgvTaiKhoan.Rows[rowindex];
            idtam = row.Cells[0].Value.ToString();
            var f = new FormTaiKhoanCapNhap {id = idtam};
            f.ShowDialog();
        }
        private void FormTaiKhoan_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            dgvTaiKhoan.DataSource = db.TaiKhoans.ToList();
            var c = dgvTaiKhoan.Columns["LoaiTaiKhoanId"] as DataGridViewComboBoxColumn;
            if (c == null) return;
            c.DataSource = db.LoaiTaiKhoans.ToList();
            c.DisplayMember = "TenLoaiTaiKhoan";
            c.ValueMember = "Id";
            Reset();
        }

        private void Delete()
        {
            if (idtam != "")
            {
                var t = db.TaiKhoans.Find(idtam);
                if (MessageBox.Show("Bạn muốn xóa nhóm: " + t.TenTaiKhoan + "!", "Xác nhận", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) != DialogResult.Yes) return;
                db.TaiKhoans.Remove(t);
                db.SaveChanges();
                LoadData();
            }
            else
            {
                MessageBox.Show("Không tải được dữ liệu. Bạn hãy chọn tài khoản cần xóa.");
            }
        }

        private void Reset()
        {
            idtam = "";
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != "")
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }
    }
}
